package com.jfm.jfmc.core.config;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author DYang
 * @since 2024-10-18 22:35
 */
@Component
public class ApplicationContextHelper implements ApplicationContextAware {

    // Spring上下文
    private static ApplicationContext context;

    @Override
    public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }

    /**
     * 获取Spring上下文
     *
     * @return Spring上下文
     */
    public static ApplicationContext getApplicationContext() {
        return context;
    }

    /**
     * 根据beanClass类名称获取bean
     *
     * @param beanClass bean名称
     * @return bean对象
     */
    public static <T> T getBean(Class<T> beanClass) {
        return context.getBean(beanClass);
    }

    /**
     * 根据bean名称获取bean
     *
     * @param beanName bean名称
     * @return bean对象
     */
    public static <T> T getBean(String beanName, Class<T> beanClass) {
        return context.getBean(beanName, beanClass);
    }
}
